<template>

  <el-row class="header-Color">
    <el-col :span="12" class="headercol-color">
      <span>首页</span>
      <span v-for="(item, index) in breadarr" :key="index">
        /{{ item }}
      </span>
    </el-col>

    <el-col :span="6" :push="6" class="headercol-color">
      <el-row>
        <el-col :span="5">
          <el-upload class="avatar-uploader" action="http://1.15.228.131:8080/tianfuHealth/uploadImage"
            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-col>
        <el-col :span="12">
          <span>欢迎你！</span>
          <span>{{ username }}</span>
        </el-col>
        <el-col :span="3">
          <i class="el-icon-setting" @click="exit">退出</i>
        </el-col>
      </el-row>


    </el-col>
  </el-row>

</template>

<script>
export default {
  props: {
    breadarr: Array
  },
  data() {
    return {
      username: '',
      imageUrl: "http://1.15.228.131:81/681efab3cc824ad5963986733800458c.jpg",
      admin: []
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      // console.log(res)
      let arr = res.data.split('/')

      if (res.code == 0) {
        this.admin.image = arr[arr.length - 1];
        this.$axios({
          method: 'post',
          url: '/admin/update',
          data: this.admin
        }).then(ress => {
          if (ress.code == 0) {
            this.$message({
              message: '头像修改成',
              type: 'success'
            });
          } else {
            this.$message.error('头像修改失败');
          }
        })
      } else {
        this.$message.error('头像修改失败');
      }
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/bmp' || file.type === 'image/gif' || file.type === 'image/webp' || file.type === 'image/svg';
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG png bmp gif webp svg格式之一');
      }
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB');
      }
      return isJPG && isLt5M;
    },

    exit() {
      // console.log(111)
      sessionStorage.removeItem('mytoken')
      sessionStorage.removeItem('uname')
      this.$router.push('/')
    }
  },
  created() {
    this.$axios({
      method: 'post',
      url: '/admin/list',
      data: { limit: '1000', page: 1, }
    }).then(res => {
      let arrs = res.data
      let uname = JSON.parse(sessionStorage.getItem('uname'))
      arrs.forEach(item => {
        if (item.username == uname) {
          // console.log(item)
          this.username = item.name
          this.imageUrl = 'http://1.15.228.131:81/' + item.image
          this.admin = item
          return false
        }
        
      })

    })
  }
}
</script>

<style>
.header-Color {
  background-color: #FFFFFF;
  height: 60px;
  line-height: 40px;
}

.headercol-color {
  padding: 5px;
}

img {
  width: 40px;
  height: 40px;
  border-radius: 20px;
}
</style>